﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace map1
{
    /// <summary> 
    /// Extends the DataGrid. 
    /// </summary> 
    public static class DataGridExtensions
    {
        /// <summary> 
        /// Gets the list of DataGridRow objects. 
        /// </summary> 
        /// <param name="grid">The grid wirhrows.</param> 
        /// <returns>List of rows of the grid.</returns> 
        public static ICollection<DataGridRow> GetRows(this DataGrid grid)
        {
            List<DataGridRow> rows = new List<DataGridRow>();

            foreach (var rowItem in grid.ItemsSource)
            {
                // Ensures that all rows are loaded. 
                grid.ScrollIntoView(rowItem, grid.Columns.Last());

                // Get the content of the cell. 
                FrameworkElement el = grid.Columns.Last().GetCellContent(rowItem);

                // Retrieve the row which is parent of given element. 
                DataGridRow row = DataGridRow.GetRowContainingElement(el.Parent as FrameworkElement);

                // Sometimes some rows for some reason can be null. 
                if (row != null)
                    rows.Add(row);
            }

            return rows;
        }
    } 
}
